//
//  UserAccount.swift
//  Weibo
//
//  Created by 孙明海 on 16/1/26.
//  Copyright © 2016年 孙明海. All rights reserved.
//

import UIKit

class UserAccount: NSObject, NSCoding {
 /// 用于调用access_token，接口获取授权后的access token。
    var access_token: String?
/// access_token的生命周期，单位是秒数。
    var expires_in: NSTimeInterval = 0 {
        didSet {
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    var expiresDate: NSDate?
    
 /// access_token的生命周期（该参数即将废弃，开发者请使用expires_in）。
    var remind_in: String?
 /// 当前授权用户的UID。
    var uid: String?
    /// 用户昵称
    var screen_name: String?
    /// 用户头像地址（大图），180×180像素
    var avatar_large: String?
    
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override var description: String {
        let key = ["access_token","expires_in","remind_in","expiresDate","uid","screen_name","avatar_large"]
        return  dictionaryWithValuesForKeys(key).description
    }
    
    //MARK: - 归档解档
    /// 解档
    required init?(coder aDecoder: NSCoder) {
        
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        
    }
    /// 归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
}


